import {
	$post
} from "@/utis/request.js"
export const login = () => {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: (loginRes) => {
				wxlogin(loginRes.code).then(res => {
					resolve(res)
				})
			},
			fail: (e) => {
				uni.showModal({
					title: '提示',
					content: '授权失败，请重新授权',
					showCancel: false,
					success: function(res) {}
				})
			}
		})
	})

}
export const wxlogin = (code) => {
	return new Promise((resolve, reject) => {
		$post('user/wxlogin', {
			code: code,
		}).then(res => {
			if (res.code == 200) {
				uni.setStorage({
					key: 'token',
					data: res.data.token
				})
				resolve(res.data.token)
			}
		})
	})
}

export const getUserInfo = () => {
	return new Promise((resolve, reject) => {
		$post('user/userinfo', {}).then(res => {
			if (res.code == 401) {
				login().then(res => {
					getUserInfo()
				})
			}
			resolve(res.data)
		})
	})
}
